<?php

require('common.php');

// stores instances based on data in URL sent from edit_instance

function insert_in_db($domain, $line, $call)
{
	$user = user();
	sql_exec("UPDATE Instances SET Line_Number=Line_Number+1 WHERE Domain='$domain' AND Line_Number >= $line AND User='$user'");
	// move down all Vals entries
	sql_exec("UPDATE Vals SET Line_Number=Line_Number+1 WHERE Domain='$domain' AND Line_Number >= $line AND User='$user'");
	sql_exec("INSERT INTO Instances SET Domain='$domain', Line_Number='$line', Fxn_Call='$call', User='$user'");
	update($user, $domain, $line);
}


$call = $G['call'];

// if there is anything special to be done, it should be done after the insertion is made

switch ($call) {
case 'if':
	insert_in_db($domain, $line, 'endif');
	insert_in_db($domain, $line, 'if');
	break;
case 'while':
	insert_in_db($domain, $line, 'endwhile');
	insert_in_db($domain, $line, 'while');
	break;
case 'for':
	insert_in_db($domain, $line, 'endfor');
	insert_in_db($domain, $line, 'for');
	break;
case 'ul':
	insert_in_db($domain, $line, 'endul');
	insert_in_db($domain, $line, 'ul');
	break;
case 'ol':
	insert_in_db($domain, $line, 'endol');
	insert_in_db($domain, $line, 'ol');
	break;
case 'select':
	insert_in_db($domain, $line, 'endselect');
	insert_in_db($domain, $line, 'select');
	break;
case 'p':
	insert_in_db($domain, $line, 'endp');
	insert_in_db($domain, $line, 'p');
	break;
case 'store_in_table':
	// store_in_table is a special case because we want to store which table we are using
	$table = $G['table'];
	insert_in_db($domain, $line, $call);
	sql_exec("UPDATE Instances SET Bonus_Field='INSERT INTO $table' WHERE User='$user' AND Domain='$domain' AND Line_Number='$line'");
	break;
case 'delete_from_table':
	// this is another special case
	$table = $G['table'];
	insert_in_db($domain, $line, $call);
	sql_exec("UPDATE Instances SET Bonus_Field='DELETE FROM $table' WHERE User='$user' AND Domain='$domain' AND Line_Number='$line'");
	break;
case 'database':
	$table = $G['table'];
	insert_in_db($domain, $line, 'enddb');
	insert_in_db($domain, $line, 'database');
	sql_exec("UPDATE Instances SET Bonus_Field='SELECT * FROM $table' WHERE User='$user' AND Domain='$domain' AND Line_Number='$line'");
	break;
case 'form':
	// very special case
	// create a function definition called form_x where x is the definition ID (to make it unique)
	// add an endform which is the submit button and the </form> tag
	// this should not be a control structure
	$result = sql_exec("INSERT INTO Definitions SET Fxn_Call='form_temp', English_Name='Untitled Form', Description='The script that\'s executed when this form is submitted.', User='$user'");
	$id = mysql_insert_id();
	$def_name = 'form_' . "$id";
	sql_exec("UPDATE Definitions SET Fxn_Call='$def_name' WHERE ID='$id' AND User='$user' ");
	// create the blank page corresponding to this form's destination function
	
	// add null variable 
	$sql = "INSERT INTO Variables SET Domain='$def_name', User='$user', Description='Null Variable' ";
	mysql_query($sql) or die("unable to $sql: " . mysql_error());
	
	insert_in_db($domain, $line, 'endform');
	insert_in_db($domain, $line, 'form');
	
	// the connection between the instance and the definition is that the definition's ID is stored in Var1 of the instance
	$sql = "UPDATE Instances SET Var1='$id' WHERE Domain='$domain' AND User='$user' AND Line_Number='$line'";
	sql_exec($sql);
	break;
default:
	insert_in_db($domain, $line, $call);
}


header("Location: " . instance_uri($call, $line));
?>
